package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityAttachment;
import cc.comac.mom.bean.QualityAttachmentGroup;
import cc.comac.mom.dao.entity.QualityAttachmentDao;
import cc.comac.mom.dao.entity.QualityAttachmentGroupDao;
import cc.comac.mom.entity.QualityAttachmentEntity;
import cc.comac.mom.entity.QualityAttachmentGroupEntity;
import cc.comac.mom.redis.IdFactory;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
public class QualityAttachmentGroupDataHandler extends ServiceImpl<QualityAttachmentGroupDao, QualityAttachmentGroupEntity>{

	@Autowired
	private QualityAttachmentGroupDao qualityAttachmentGroupDao;
	
	@Autowired
	private QualityAttachmentDao qualityAttachmentDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	@Autowired
	private IdFactory idFactory;
	
	/**
	 * 通过attachmentId集合和组名称新增组返回组id
	 * **/ 
	public long getGroupIdByAttachmentIds(List<Long> attchmentIds,String groupName) {
		List<QualityAttachmentGroupEntity> arrayList = new ArrayList<QualityAttachmentGroupEntity>();
		long generateId = idFactory.generateId();
		for (long attchmentId : attchmentIds) {
			QualityAttachmentGroupEntity qualityAttachmentGroupEntity = new QualityAttachmentGroupEntity();
			qualityAttachmentGroupEntity.setAttachmentId(attchmentId);
			qualityAttachmentGroupEntity.setGroupId(generateId);
			qualityAttachmentGroupEntity.setGroupName(groupName);
			baseEntityDataUtil.setNotNullBaseEntityData(qualityAttachmentGroupEntity);
			arrayList.add(qualityAttachmentGroupEntity);
		}
		this.saveBatch(arrayList, arrayList.size());
		return generateId;
	}
	
	
	public ResultBean insertBatch(List<QualityAttachmentGroup> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<QualityAttachmentGroupEntity> listEntity = new ArrayList<QualityAttachmentGroupEntity>();
		for(QualityAttachmentGroup item :list) {
			QualityAttachmentGroupEntity innerData = item.getInnerData();
			if(innerData == null) {
				throw new RuntimeException("数据为空");
			}
			if(innerData.getAttachmentId() == 0) {
				throw new RuntimeException("附件id不存在");
			}
			if(innerData.getGroupId() == 0) {
				throw new RuntimeException("附件组id不存在");
			}
			baseEntityDataUtil.setBaseEntityData(innerData);
			listEntity.add(innerData);
		}
		boolean saveBatch = this.saveBatch(listEntity);
		if(!saveBatch) {	
			throw new RuntimeException("附件组保存失败");
		}
		return resultBean;
	}
	
	public List<Long> getAttachIdByGroupId(long groupId){
		List<Long> list = new ArrayList<Long>();
		QueryWrapper<QualityAttachmentGroupEntity> queryWrapper = new QueryWrapper<QualityAttachmentGroupEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("group_id", groupId);
		List<QualityAttachmentGroupEntity> selectList = qualityAttachmentGroupDao.selectList(queryWrapper);
		if (selectList.size() > 0) {
			list = selectList.stream().map(item -> item.getAttachmentId()).collect(Collectors.toList());
		}
		return list;
	}
	
	public List<QualityAttachmentGroup> getByAttachmentGroupId(long groupId){
		HashMap<String,Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("group_id", groupId);
		List<QualityAttachmentGroupEntity> selectByMap = qualityAttachmentGroupDao.selectByMap(map);
		if(selectByMap == null || selectByMap.size()<1) {
			return null;
		}
		ArrayList<QualityAttachmentGroup> list = new ArrayList<QualityAttachmentGroup>();
		for(QualityAttachmentGroupEntity item :selectByMap) {
			QualityAttachmentGroup qualityAttachmentGroup = new QualityAttachmentGroup();
			qualityAttachmentGroup.setInnerData(item);
			QualityAttachmentEntity attachmentEntity = qualityAttachmentDao.selectById(item.getAttachmentId());
			if(attachmentEntity != null) {
				QualityAttachment qualityAttachment = new QualityAttachment();
				qualityAttachment.setInnerData(attachmentEntity);
				qualityAttachmentGroup.setQualityAttachment(qualityAttachment);
			}
			list.add(qualityAttachmentGroup);
		}
		return list;
		
	}
	
	
	// 通过附件组id物理删除对应附件组及附件信息
	public void removeByGroupId(long groupId) {
		QueryWrapper<QualityAttachmentGroupEntity> queryWrapper = new QueryWrapper<QualityAttachmentGroupEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("group_id", groupId);
		List<QualityAttachmentGroupEntity> selectList = qualityAttachmentGroupDao.selectList(queryWrapper);
		if (selectList.size() > 0) {
			// 附件主键的集合
			List<Long> idsList = new ArrayList<Long>();
			for (QualityAttachmentGroupEntity qualityAttachmentGroupEntity : selectList) {
				idsList.add(qualityAttachmentGroupEntity.getAttachmentId());
			}
			
			// 物理删除附件对应数据
			qualityAttachmentDao.deleteBatchIds(idsList);
			
			// 物理删除附件组对应数据
			qualityAttachmentGroupDao.deleteBatchIds(idsList);
		}
	}
	
	// 通过附件组id集合物理删除对应附件组及附件信息
		public void removeByGroupId(List<Long> groupIds) {
			if (groupIds != null && groupIds.size() > 0) {
				QueryWrapper<QualityAttachmentGroupEntity> queryWrapper = new QueryWrapper<QualityAttachmentGroupEntity>();
				queryWrapper.eq("row_status", 1);
				queryWrapper.in("group_id", groupIds);
				List<QualityAttachmentGroupEntity> selectList = qualityAttachmentGroupDao.selectList(queryWrapper);
				if (selectList.size() > 0) {
					// 附件主键的集合
					List<Long> idsList = new ArrayList<Long>();
					for (QualityAttachmentGroupEntity qualityAttachmentGroupEntity : selectList) {
						idsList.add(qualityAttachmentGroupEntity.getAttachmentId());
					}

					// 物理删除附件对应数据
					qualityAttachmentDao.deleteBatchIds(groupIds);

					// 物理删除附件组对应数据
					qualityAttachmentGroupDao.deleteBatchIds(idsList);
				}
			}
		}
		
		public QualityAttachment selectByAttachmentId(long attachmentId) {
			QualityAttachmentEntity selectById = qualityAttachmentDao.selectById(attachmentId);
			QualityAttachment qualityAttachment = new QualityAttachment();
			qualityAttachment.setInnerData(selectById);
			return qualityAttachment;
		}

}
